//
//  SearchFriendsViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-3-2.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "SearchFriendsViewController.h"
#import "SVProgressHUD.h"
#import "AppsEngine.h"
#import "PersonalInfoViewController.h"

@interface SearchFriendsViewController ()<UITableViewDataSource,UITableViewDelegate> {
    NSMutableArray *searchResultList;
}

@end

@implementation SearchFriendsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"查找好友"];
    [super customBackButton];
    searchResultList = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Button Action
- (IBAction)btnAction:(id)sender {
    if (tfSearch.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入搜索内容!"];
        return;
    }
    
    [viewLoading setFrame:CGRectMake(0, viewSearch.frame.origin.y + viewSearch.frame.size.height + 20, viewLoading.frame.size.width, viewLoading.frame.size.height)];
    [clActivityIndicator startAnimating];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:tfSearch.text,@"keyword", nil];
    [[AppsEngine shareAppsEngine] submitRequest:@"gfSearchFriend" param:dict onCompletion:^(id jsonResponse) {
        searchResultList = [jsonResponse objectForKey:@"SearchList"];
        [clActivityIndicator stopAnimating];
        if ([searchResultList respondsToSelector:@selector(count)] && [searchResultList count] > 0) {
            [viewLoading removeFromSuperview];
            [tbSearchList setHidden:NO];
            [tbSearchList reloadData];
        }
        else {
            //没有结果
            labelStatus.text = @"没有符合条件的好友";
        }
    } onError:^(NSError *error) {
        [clActivityIndicator stopAnimating];
        labelStatus.text = @"网络连接超时";
    } method:@"GET"];
    
}

- (IBAction)hideKeyboard:(id)sender {
    [tfSearch resignFirstResponder];
}


#pragma mark - UITableViewDelegate
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [searchResultList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"ContactBookCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(!cell){
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ContactBookCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.tag = indexPath.row;//区分奇偶
    if([cell respondsToSelector:@selector(setDataInfo:)]){
        [cell performSelector:@selector(setDataInfo:) withObject:[searchResultList objectAtIndex:indexPath.row]];
    }
    
    [cell setTag:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"--- current selected index:%d",indexPath.row);
    [tbSearchList deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[searchResultList objectAtIndex:indexPath.row]];
    [dict setObject:[dict objectForKey:@"uId"] forKey:@"userId"];
    [dict removeObjectForKey:@"uId"];
    [dict setObject:[dict objectForKey:@"id"] forKey:@"recordId"];
    [dict removeObjectForKey:@"id"];
    PersonalInfoViewController *picv = [[PersonalInfoViewController alloc] init];
    picv.userInfo = dict;
    [self.navigationController pushViewController:picv animated:YES];
}

@end
